package com.ruoyi.classai.api;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.classai.domain.EwcResource;
import com.ruoyi.classai.domain.EwcResourceDetails;
import com.ruoyi.classai.domain.dto.resource.EwcResourceDetailsQueryDto;
import com.ruoyi.classai.domain.dto.resource.EwcResourceQueryDto;
import com.ruoyi.classai.domain.vo.resource.EwcResourceDetailsVo;
import com.ruoyi.classai.service.IEwcResourceDetailsService;
import com.ruoyi.classai.service.IEwcResourceService;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.system.api.RemoteDictService;
import com.ruoyi.system.api.domain.SysDictData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Objects;

/**
 * 资源库接口
 *
 * @author lcj
 * @date 2025年03月21日 11:56
 */
@Slf4j
@Validated
@RestController
@Tag(name = "资源库管理接口", description = "资源库管理接口")
@RequestMapping("/api/resource")
public class ResourceApi extends BaseController {

    @Autowired
    private IEwcResourceService resourceService;
    @Autowired
    private IEwcResourceDetailsService resourceDetailsService;
    @Autowired
    private RemoteDictService dictService;

    /**
     * 查询字典表类型
     *
     * @param dictType 字典表类型
     * @return 字典表信息
     */
    @Operation(summary = "查询字典表类型")
    @GetMapping("/getDictType")
    public R<List<SysDictData>> getDictType(@NotBlank(message = "字典表类型不能为空") String dictType) {
        return dictService.getDictData(dictType, SecurityConstants.INNER);
    }

    /**
     * 查询字典值详情
     *
     * @param dictType  字典表类型
     * @param dictValue 字典表值
     * @return 字典表信息
     */
    @Operation(summary = "查询字典值详情")
    @GetMapping("/getDictLabel")
    public R<String> getDictLabel(@NotBlank(message = "字典表类型不能为空") String dictType,
                                  @NotBlank(message = "字典表值不能为空") String dictValue) {
        return dictService.getDictLabel(dictType, dictValue, SecurityConstants.INNER);
    }


    /**
     * 查询资源库列表
     *
     * @param resourceName 资源名称
     * @param gradeType    年级类型
     * @param subjectType  学科类型
     * @return 资源库
     */
    @Operation(summary = "查询资源库列表")
    @GetMapping("/getResourceList")
    public TableDataInfo getResourceList(String resourceName, Integer gradeType, Integer subjectType) {
        startPage();
        List<EwcResource> list = resourceService.list(Wrappers.<EwcResource>lambdaQuery().like(StringUtils.isNotEmpty(resourceName), EwcResource::getResourceName, resourceName)
                .eq(Objects.nonNull(gradeType), EwcResource::getGradeType, gradeType)
                .eq(Objects.nonNull(subjectType), EwcResource::getSubjectType, subjectType)
                .eq(EwcResource::getStatus, 1));
        return getDataTable(list);
    }


    /**
     * 查询资源库详情
     *
     * @param resourceId 资源id
     * @return 资源库
     */
    @Operation(summary = "查询资源库详情")
    @GetMapping("/getResourceInfo")
    public AjaxResult getResourceList(@NotBlank(message = "资源ID不能为空") String resourceId) {
        EwcResource resource = resourceService.getResourceById(resourceId);
        List<EwcResourceDetails> list = resourceDetailsService.list(Wrappers.<EwcResourceDetails>lambdaQuery().eq(EwcResourceDetails::getResourceId, resourceId));
        return AjaxResult.success("查询成功", new JSONObject().set("resource", resource)
                .set("resourceList", list));
    }

    /**
     * 查询资源库列表
     *
     * @param dto
     * @return
     */
    @Operation(summary = "查询通用资源库列表")
    @PostMapping("/resourceList")
    public AjaxResult resourceList(@RequestBody EwcResourceQueryDto dto) {
        List<EwcResource> list = resourceService.selectResourceList(dto);
        return AjaxResult.success("操作成功！", list);
    }

    @Operation(summary = "查询通用资源库详情列表")
    @PostMapping("/resourceDetailsList")
    public AjaxResult resourceDetailsList(@RequestBody EwcResourceDetailsQueryDto dto) {
        List<EwcResourceDetailsVo> list = resourceDetailsService.resourceDetailsList(dto);
        return AjaxResult.success("操作成功！", list);
    }

}
